import validateInstance from "@/libs/validate/validate";

// 判断纯中文
const isPureChinese = (str: string) => {
  var pattern = /[\u4E00-\u9FA5]/;
  return pattern.test(str);
}
// 判断大写字母
const isAllUpperCase = (str: string) => {
  return /^[A-Z]+$/.test(str);
}
// 判断数字
const isNumeric = (str: string) => {
  return /^\d+$/.test(str);
}
// 获取GBK编码下的字符串字节数
const getGBKStringByteCount = (str: string) => {
  let count = 0;
  const strs = str?.split?.('') || [];
  strs.forEach(x => {
    if (isPureChinese(x)) {
      count += 2;
    } else {
      count++;
    }
  })
  return count;
}

// 姓名匹配
const namePattern: any = {
  // 纯中文
  pureChinese(str: string) {
    return isPureChinese(str);
  },
  // 纯中文加字母
  pureChineseWithUpperCase(str: string) {
    return str?.split?.('')?.every?.(code => isPureChinese(code) || isAllUpperCase(code));
  },
  // 纯中文加符号'·'
  pureChineseWithDot(str: string) {
    return str?.split?.('')?.every?.(code => isPureChinese(code) || (code === '·'));
  },
  // 纯大写字母
  pureUpperCase(str: string) {
    return isAllUpperCase(str);
  },
  // 大写字母符号'.'、非连续空格的组合
  pureUpperCaseWithPeriodAndNonContinuousSpaces(str: string) {
    let strs = str?.split?.('') || [];
    return strs.every?.((code, index: number) => {
      // 非连续空格
      let nonContinuousSpaces = true;
      // 非被空格包围'.'
      let nonAroundSpacesDot = true;
      if (code === ' ' && (strs[index - 1] === ' ' || strs[index - 1] === ' ')) {
        nonContinuousSpaces = false;
      }
      if (code === '.' && (strs[index - 1] === ' ' || strs[index - 1] === ' ')) {
        nonAroundSpacesDot = false;
      }
      return (isAllUpperCase(code) || (code === '.') || (code === ' ')) && nonContinuousSpaces && nonAroundSpacesDot;
    });
  },
}

// 证件号匹配
const idnoPattern: any = {
  idCard(str: string) {
    const reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    return reg.test(str);
  },
  passport(str: string) {
    const reg = /^[a-zA-Z0-9]{5,17}$/;
    return reg.test(str);
  },
  homeReturnPermitOrTaiwanCompatriotCertificate(str: string) {
    let bol = false;
    if (str.length === 8 && str?.split?.('')?.every?.(code => isNumeric(code))) {
      bol = true;
    }
    const strFirstLetter = str.substring(0, 1);
    const strNonFirstLetter = str.substring(1);
    if (['H', 'M'].includes(strFirstLetter) && strNonFirstLetter.length >= 8 && strNonFirstLetter.length <= 11 && strNonFirstLetter?.split?.('')?.every?.(code => isNumeric(code))) {
      bol = true;
    }
    return bol;
  },
  HMTIdCard(str: string) {
    const reg = /^8[123]0000(?:19|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}[\dX]$/;
    return reg.test(str);
  },
  HMPassport(str: string) {
    const strFirstLetter = str.substring(0, 1);
    const strSecondLetter = str.substring(1, 2);
    const strOtherLetter = str.substring(2);
    return ['W', 'C'].includes(strFirstLetter) && (isAllUpperCase(strSecondLetter) || isNumeric(strSecondLetter)) && strOtherLetter.length === 7 && isNumeric(strOtherLetter);
  },
  TPassport(str: string) {
    return str.length === 9 && str?.split?.('')?.every?.(code => isAllUpperCase(code) || isNumeric(code));
  },
}

const patternForCardType = [
  {
    id_type: '111',
    namePattern: ['pureChinese', 'pureChineseWithDot'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加符号'·'`,
    idnoPattern: ['idCard'],
    idnoPatternErrMsg: `证件号码格式错误`,
  },
  {
    id_type: '07',
    namePattern: ['pureChinese', 'pureChineseWithUpperCase', 'pureChineseWithDot', 'pureUpperCase', 'pureUpperCaseWithPeriodAndNonContinuousSpaces'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加字母、纯中文加符号'·'、纯大写字母、大写字母符号'.'、非连续空格的组合`,
    idnoPattern: ['passport'],
    idnoPatternErrMsg: `护照支持5-17位大写字母和数字，或纯数字，或纯大写字母`,
  },
  {
    id_type: '414',
    namePattern: ['pureChinese', 'pureChineseWithUpperCase', 'pureChineseWithDot', 'pureUpperCase', 'pureUpperCaseWithPeriodAndNonContinuousSpaces'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加字母、纯中文加符号'·'、纯大写字母、大写字母符号'.'、非连续空格的组合`,
    idnoPattern: ['passport'],
    idnoPatternErrMsg: `护照支持5-17位大写字母和数字，或纯数字，或纯大写字母`,
  },
  {
    id_type: '港澳回乡证或台胞证',
    namePattern: ['pureChinese', 'pureChineseWithDot'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加符号'·'`,
    idnoPattern: ['homeReturnPermitOrTaiwanCompatriotCertificate'],
    idnoPatternErrMsg: `证件号应为8位的纯数字(台胞证)、H或M+8至11位数字(港澳回乡证)`,
  },
  {
    id_type: '港澳台居民居住证',
    namePattern: ['pureChinese', 'pureChineseWithDot'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加符号'·'`,
    idnoPattern: ['HMTIdCard'],
    idnoPatternErrMsg: `证件号码格式错误`,
  },
  {
    id_type: '港澳通行证',
    namePattern: ['pureChinese', 'pureChineseWithDot'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加符号'·'`,
    idnoPattern: ['HMPassport'],
    idnoPatternErrMsg: `证件号应为W或C开头 + 1位字母或数字 + 7位阿拉伯数字`,
  },
  {
    id_type: '往来台湾通行证',
    namePattern: ['pureChinese', 'pureChineseWithDot'],
    namePatternErrMsg: `姓名应为纯中文、纯中文加符号'·'`,
    idnoPattern: ['TPassport'],
    idnoPatternErrMsg: `证件号应为9位大写字母和阿拉伯数字组合`,
  },
]
export default {
  checkName: (x: any) => {
    if (getGBKStringByteCount(x.real_name) < 4) {
      return '被保人姓名格式不对';
    }
    const patternForCardTypeTypeItem = patternForCardType?.find?.(y => y.id_type === x.id_type);
    return patternForCardTypeTypeItem?.namePattern?.some(y => namePattern[y](x.real_name)) ? '' : (patternForCardTypeTypeItem?.namePatternErrMsg || '');
  },
  checkIdno: (x: any) => {
    // if (!x.id_no) {
    //   return '证件号码不能为空';
    // }
    // if (!validateInstance.test(x.id_no, "idcard")) {
    //   return '证件号码格式不对';
    // }
    const patternForCardTypeTypeItem = patternForCardType?.find?.(y => y.id_type === x.id_type);
    return patternForCardTypeTypeItem?.idnoPattern?.some(y => idnoPattern[y](x.id_no)) ? '' : (patternForCardTypeTypeItem?.idnoPatternErrMsg || '');
  },
  checkRepeat: (x: any, list: any[]) => {
    return  list.filter(y => y.id_no === x.id_no).length > 1 ? "被保人证件号不能重复" : "";
  }

}